<template>
    <div class="goods-container">

        <transition
            @before-enter="beforeEnter"
            @enter="enter"
            @after-enter="afterEnter"
        >
            <div class="ball" v-show="flag" ref="ball"></div>
        </transition>
        

        <!-- 商品轮播图 -->
        <div class="mui-card">
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                    <carousel-box :carousel="carousel" :isfull="false"></carousel-box>
                    <!-- <mt-swipe :auto="4000">
                        <mt-swipe-item  v-for="item in carousel" :key="item.id">
                            <img :src="item.img" alt="">
                        </mt-swipe-item>            
                    </mt-swipe> -->
                </div>
            </div>            
        </div>

        <!-- 商品购购买信息 -->
        <div class="mui-card buy">
            <div class="mui-card-header">{{ this.goodsInfo.title }}</div>
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                    <p class="price">
                        市场价：<span class="old_price"><del>￥{{ this.goodsInfo.market_price }} </del></span>
                        销售价：<span class="new_price">￥{{ this.goodsInfo.sell_price }}</span>                
                    </p>
                    <p>
                        购买数量：
                        <numbox :valmax="goodsInfo.stock_quantity" @getSelectedCount="getSelectedCount">
                        </numbox>                        
                    </p>
                    <p>
                        <mt-button type="primary" size="small">立即购买</mt-button>
                        <mt-button type="danger" size="small" @click="addShopcar">加入购物车</mt-button>
                    </p>
                </div>
            </div>            
        </div>


        <!-- 商品参数信息 -->
        <div class="mui-card">
            <div class="mui-card-header">{{this.goodsInfo.title}}</div>
            <div class="mui-card-content">
                <div class="mui-card-content-inner">
                    <p>商品货号：{{ this.goodsInfo.goods_no }}</p>
                    <p>库存情况：{{ this.goodsInfo.stock_quantity }} 件</p>
                    <p>上市时间：{{ this.goodsInfo.add_time  }}</p>
                </div>
            </div>
            <div class="mui-card-footer">
                <mt-button type="primary" size="large" plain @click="viewDetail">图文介绍</mt-button>
                
                <mt-button type="danger" size="large" plain @click="viewComment">添加评论</mt-button>
            </div>
        </div>        

    </div>
</template>


<script>
import carousel from '../subcomponents/Carousel.vue'
import numbox from './goodsinfo_numbox.vue'

export default {
    data() {
        return {
            id: this.$route.params.id,
            carousel: [],   // 轮播图
            goodsInfo: {},  // 商品信息（
            flag: false,    // 显示标识
            selectCnt: 1    // 选择的商品数量

        }
    },
    created () {
        this.getPics(); // 获取商品轮播图
        this.getGoodsInfo();    // 获取商品信息        
    },
    methods: {
        // 获取轮播图
        getPics() {
            this.$http.get('api/getthumimages/' + this.id).then(result => {
                if (result.body.status === 0) {                    
                    result.body.message.forEach((item, index) => {
                        item.id = index;
                        item.img = item.src;
                    })

                    this.carousel = result.body.message;
                    // console.log(this.pics)
                }                
            })
        },
        // 获取商品信息
        getGoodsInfo() {
            this.$http.get('api/goods/getinfo/' + this.id).then(result => {
                if (result.body.status === 0) {
                    this.goodsInfo = result.body.message[0];
                    console.log(this.goodsInfo)
                }
            })
        },

        // 进入图文介绍页面
        viewDetail() {
            this.$router.push({
                name: "goodsdesc",
                params: {
                    id: this.id
                }
            })
        },
        // 进入评论介绍页面
        viewComment() {
            this.$router.push({
                name: "goodscomment",
                params: {
                    id: this.id
                }
            })
        },

        // 添加到购物车
        addShopcar() {
            this.flag = !this.flag;

            // { id: 商品id, count: 商品数量, price: 商品价格, selected：true 是否选择 }
            this.$store.commit('addToShopCar', {
                id: this.id,
                price: this.goodsInfo.sell_price,
                info: this.goodsInfo,
                count: this.selectCnt,
                selected: true
            });

        },

        // 购物车小球
        beforeEnter(el) {
            // 开始动画之前的起始样式
            // 设置小球开始动画之前的起始位置
            el.style.transform = "translate(0px, 0px)"
        },
        enter(el, done) {
            // 这句话没有时间的作用，但是如果不写，出不来动画效果
            // 可以认为el.offsetWidth 会强制动画刷新
            el.offsetWidth

            // enter 表示动画，开始之后的样式，这里，可以设置小球
            // 完成动画之后的结束状态

            // 不同的分辨率的设备会不同，所以写死位移会出bug
            // el.style.transform = "translate(80px, 416px)";

            // 获取小球 页面中的位置
            const ballPosition = this.$refs.ball.getBoundingClientRect();
            // 获取购物车徽标的页面位置
            const shopcarPosition = document.getElementById("badge-shopcar").getBoundingClientRect();

            const xDist = shopcarPosition.left - ballPosition.left;
            const yDist = shopcarPosition.top - ballPosition.top;

            el.style.transform = `translate(${xDist}px, ${yDist}px)`;
            console.log(el.style.transform)

            // 设置动画效果并查看效果展示
            // https://cubic-bezier.com/#.14,-0.26,1,.4
            el.style.transition = "all 0.5s cubic-bezier(.14,-0.26,1,.4)";

            // 这里的done，相当于调用 afterEnter 函数
            done();
        },
        afterEnter(el) {
            // 动画完成之后，会调用 afterEnter
            this.flag = !this.flag;
        },

        // 获取选择的商品数量
        getSelectedCount(val) {
            this.selectCnt = val;
            console.log("添加到购物车:" + val);
        }


    },
    components: {
        'carousel-box': carousel,
        numbox
    }
}
</script>


<style lang="scss" scoped>
.goods-container {
    background-color: #eee;
    overflow: hidden;

    // .mint-swipe {
    //     height: 200px;
    // }

    // .carousel {
    //     height: 200px;
    // } 

    // .mui-numbox {
    //     .ball-box {
    //         position: fixed;
    //     }
    // }

    .ball {
        background-color: red;
        width: 15px;
        height: 15px;
        border-radius: 50%;
        position: absolute;
        z-index: 80;

        left: 150px;
        top: 390px;
        // transform: translate(88px, 150px);
    }

    .buy {        
        
        .price {
            .old_price {
                
            }

            .new_price {
                color: red;
                font-size: 16px;
                font-weight: bold;
            }
        }
    }

    .info {
        
    }

    .mui-card-footer {
        display: block;
        button {
            margin: 10px 0;
        }
    }
}
</style>
